package com.xzr.hospitalregister.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xzr.hospitalregister.R;
import com.xzr.hospitalregister.entity.DthzListItem;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/24 0024.
 */

public class DthzItemAdapter extends RecyclerView.Adapter<DthzItemAdapter.MyHolder>{
    private List<DthzListItem> data;
    private Context context;
    public DthzItemAdapter(List<DthzListItem> data, Context context){
        this.data = data;
        this.context = context;
    }
    private OnClickListener onClickListener;
    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onLongClickListener(int position);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_recycler_dthz, null));
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        holder.edittext_yisheng.setText(data.get(position).getYiSheng());
        holder.edittext_zhenshi.setText(data.get(position).getZhenShi());
        List<String>  bingRenAll=new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(data.get(position).getPatient());
            for (int i = 0; i <jsonArray.length() ; i++) {
                bingRenAll.add(jsonArray.getJSONObject(i).getString("NAME"));
            }
            AAAA adapter=new AAAA(bingRenAll,context);
            holder.bingRenAll.setLayoutManager(new LinearLayoutManager(context));
            holder.bingRenAll.setAdapter(adapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onClickListener.onLongClickListener(position);
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyHolder extends RecyclerView.ViewHolder{
        TextView edittext_yisheng,edittext_zhenshi;
        RecyclerView bingRenAll;
        public MyHolder(View itemView) {
            super(itemView);
            edittext_yisheng = (TextView) itemView.findViewById(R.id.edittext_yisheng);
            edittext_zhenshi = (TextView) itemView.findViewById(R.id.edittext_zhenshi);
            bingRenAll = (RecyclerView) itemView.findViewById(R.id.edittext_bingRenAll);
        }
    }
}
